package program;

import java.io.File;

import kernel.FileManager;
import kernel.exception.OSException;
import kernel.process.AbstractProcess;
import kernel.process.AbstractProcessHeader;

/**
 * Rm process.
 * For more see man() method.
 * @author Petr Hajek, Veronika Vencurikova
 *
 */
public class Rm extends AbstractProcess {

	public Rm(AbstractProcessHeader header, long pid) {
		super(header, pid);
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	protected String doJob(String input) throws OSException {
		if (this.params.size() < 1) throw new OSException(OSException.NO_ARGS);
		File[] files = new File[this.params.size()];
		for (int i = 0; i < this.params.size(); i++) {
			File path = absPath(this.params.get(i), shellID);
			if (FileManager.existsDir(path)) throw new OSException(OSException.ERR_DEL_DIR);
			files[i] = path;
		}
		
		for (int i = 0; i < this.params.size(); i++) {
			FileManager.deleteFile(files[i]);
		}
		return "";
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public String continueJob() throws OSException {
		this.shouldEnd = true;
		return null;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected String finalizeJob() throws OSException {
		return null;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean needParams() {
		return false;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void kill() throws OSException {
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String man(String command) {
	String man = "";
		
		man += "    removeS files\n";
		man += "\n";
		man += "Command syntax:\n";
		man += "    rm [FILE]\n";
		man += "\n";
		man += "Examples:\n";
		man += "    rm test.txt\n";
		
		return man;
	}

}
